package com.hongshan.wallet.core.login

import android.app.Activity
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.databinding.ObservableField
import android.databinding.ViewDataBinding
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.hongshan.wallet.BR
import com.hongshan.wallet.R
import com.hongshan.wallet.base.BaseOnPropertyChangedCallback
import com.hongshan.wallet.base.MyActivity
import com.hongshan.wallet.router.RouterIndex
import com.hongshan.wallet.service.WebSocketService
import com.hongshan.wallet.util.MyActivityManager
import com.jaeger.library.StatusBarUtil
import kotlinx.android.synthetic.main.activity_login.*

/**
 * Copyright Qiandai Ltd.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.register
 * @author   : Administrator
 * @data     : 2019/3/18
 * @email    : zhangxx@baihang-china.com
 */
@Route(path = RouterIndex.LOGIN)
class LoginActivity : MyActivity<ViewDataBinding, LoginViewModel>() {

    val loginVm by lazy { ViewModelProviders.of(this).get(LoginViewModel::class.java) }

    override fun initVariableId() = BR.loginVm

    override fun initContentView(savedInstanceState: Bundle?) = R.layout.activity_login

    override fun initViewModel() = loginVm

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MyActivityManager.activityStack.filter { it != this }.forEach { it.finish() }
        StatusBarUtil.setColor(this, resources.getColor(R.color.transparent_white))
        stopService(Intent(this,WebSocketService::class.java))
        loginVm.sendStatus.addOnPropertyChangedCallback(BaseOnPropertyChangedCallback<ObservableField<Boolean>> { sender, _ ->
            val result = sender.get()!!
            tvSend.isEnabled = result
            flSend.setBackgroundColor(if (result) resources.getColor(R.color.colorAccent) else resources.getColor(R.color.lineGray))
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && data != null) {
            loginVm.tel.set(data.getStringExtra("tel"))
        }
    }
}